package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.commom.Result;
import com.example.demo.entity.LendRecord;
import com.example.demo.mapper.LendRecordMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 借阅管理控制器
 */
@RestController
@RequestMapping("/LendRecord")
@Api(tags = "借阅管理信息", description = " 0328 王晓月")
public class LendRecordController {
    @Resource
    private LendRecordMapper lendRecordMapper;

    /**
     * 批量删除记录
     *
     * @param lendRecords 借出记录
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping("/deleteRecords")
    @ApiOperation(value = "批量删除记录")
    public Result<?> deleteRecords(@RequestBody List<LendRecord> lendRecords) {
        for (LendRecord record : lendRecords) {
            Map<String, Object> map = new HashMap<>();
            map.put("isbn", record.getIsbn());
            map.put("borrownum", record.getBorrownum());
            lendRecordMapper.deleteByMap(map);
        }
        return Result.success();
    }

    /**
     * 保存借阅记录
     *
     * @param lendRecord 借出记录
     * @return {@link Result }<{@link ? }>
     */
    @PostMapping
    @ApiOperation(value = "保存借阅记录")
    public Result<?> save(@RequestBody LendRecord lendRecord) {
        lendRecordMapper.insert(lendRecord);
        return Result.success();
    }

    /**
     * 查找页面
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @param search1  搜索1
     * @param search2  搜索2
     * @param search3  搜索3
     * @return {@link Result }<{@link ? }>
     */
    @GetMapping
    @ApiOperation(value = "分页查询借阅状态")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search1,
                              @RequestParam(defaultValue = "") String search2,
                              @RequestParam(defaultValue = "") String search3) {
        LambdaQueryWrapper<LendRecord> wrappers = Wrappers.<LendRecord>lambdaQuery();
        if (StringUtils.isNotBlank(search1)) {
            wrappers.like(LendRecord::getIsbn, search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrappers.like(LendRecord::getBookname, search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrappers.like(LendRecord::getReaderId, search3);
        }
        Page<LendRecord> LendRecordPage = lendRecordMapper.selectPage(new Page<>(pageNum, pageSize), wrappers);
        return Result.success(LendRecordPage);
    }

    @PutMapping("/{isbn}")
    @ApiOperation(value = "更新借阅记录")
    public Result<?> update(@PathVariable String isbn, @RequestBody LendRecord lendRecord) {
        lendRecord.setIsbn(isbn);
        lendRecordMapper.update(lendRecord);
        return Result.success();
    }
}



